/**
 * Created by SCR on 2017/7/9.
 */
/*jshint esversion: 6 */
var Util = require('./util');
// var MSAggreJS = null;
var CanvasJS = null;
var SolutionLibraryJS = null;
var TaskListJS = null;
var SolutionJS = null;
var NetworkJS = null;
var VisualJS = null;
var VisualDetailJS = null;

$().ready(function () {

    // 通用方法
    for(var key in Util){
        window[key] = Util[key];
    }

    var url = location.href;
    var solution = null;
    var task = null;

    // 用到的才加载，不然会变很慢
    if(url.indexOf('integration/network')!=-1){
        NetworkJS = require('./network/NetworkJS');
    }
    else if(url.indexOf('integration/solution') != -1 || url.indexOf('integration/task') != -1){
        // MSAggreJS = require('./integration/MSAggreJS');
        CanvasJS = require('./integration/CanvasJS');
        SolutionLibraryJS = require('./integration/SolutionLibraryJS');
        TaskListJS = require('./integration/TaskListJS');
        SolutionJS = require('./integration/SolutionJS');
    }
    else if(url.indexOf('visualization/query')!= -1){
        VisualJS = require('./visualization/VisualLibJS');
    }
    else if(url.indexOf('visualization/detail') != -1){
        VisualDetailJS = require('./visualization/VisualDetailJS');
    }

    if(url.indexOf('integration/solution/new') != -1){
        $('#saveas-solution-tool').remove();
        CanvasJS.init('edit','solution');

        SolutionJS.init();

        // new Promise((resolve, reject)=> {
        //     MSAggreJS.getALLMS((err, mss)=> {
        //         if(err){
        //             reject(err);
        //         }
        //         else{
        //             resolve(mss);
        //         }
        //     });
        // })
        //     .then((mss)=> {
        //         return new Promise((reject)=> {
        //             MSAggreJS.buildMSListModal(mss, false, (err) => {
        //                 if(err){
        //                     reject(err);
        //                 }
        //             });
        //         });
        //     })
        //     .catch((err)=> {
        //         var errMsg = '<pre>'+JSON.stringify(err,null,4)+'</pre>';
        //         $.gritter.add({
        //             title: '警告：',
        //             text: errMsg,
        //             sticky: false,
        //             time: 2000
        //         });
        //         return;
        //     });
    }

    else if(url.indexOf('integration/solution/query') != -1){
        SolutionLibraryJS.init();
    }

    else if(url.indexOf('integration/solution/detail') != -1){
        CanvasJS.init('view','solution');

        solution = $('#solution-detail').text().slice(1,-1);
        if(!solution || solution == ''){
            $.gritter.add({
                title: 'Warning:',
                text: 'Can\'t find this solution' ,
                sticky: false,
                time: 2000
            });
            return ;
        }
        solution = JSON.parse(solution);

        CanvasJS.initImport('SOLUTION',solution);
        CanvasJS.importSolution();
    }

    else if(url.indexOf('integration/solution/edit') != -1){
        CanvasJS.init('edit','solution');

        solution = $('#solution-detail').text().slice(1,-1);
        if(!solution || solution == ''){
            $.gritter.add({
                title: 'Warning:',
                text: 'Can\'t find this solution' ,
                sticky: false,
                time: 2000
            });
            return ;
        }
        solution = JSON.parse(solution);

        SolutionJS.importLayoutBySolution(solution);
        // MSAggreJS.importLayoutBySolution(solution);
        CanvasJS.initImport('SOLUTION',solution);
        CanvasJS.importSolution();
        $('#toggle-model-node-sidebar-btn').click();
        $('#toggle-cart-sidebar-btn').click();
    }

    else if(url.indexOf('integration/task/query') != -1){
        TaskListJS.init();
    }

    else if(url.indexOf('integration/task/new') != -1){
        $('#saveas-task-tool').remove();
        CanvasJS.init('configure','task');

        solution = $('#solution-detail').text().slice(1,-1);
        if(!solution || solution == ''){
            $.gritter.add({
                title: 'Warning:',
                text: 'Can\'t find this solution' ,
                sticky: false,
                time: 2000
            });
            return ;
        }
        solution = JSON.parse(solution);

        CanvasJS.initImport('SOLUTION',solution);
        CanvasJS.importSolution();
    }

    else if(url.indexOf('integration/task/detail') != -1){
        CanvasJS.init('view','task');

        task = $('#task-detail').text().slice(1,-1);
        if(!task || task == ''){
            $.gritter.add({
                title: 'Warning:',
                text: 'Can\'t find this solution' ,
                sticky: false,
                time: 2000
            });
            return ;
        }
        task = JSON.parse(task);

        CanvasJS.initImport('TASK',task);
        CanvasJS.importTask();
    }

    else if(url.indexOf('integration/task/edit') != -1){
        CanvasJS.init('configure','task');

        task = $('#task-detail').text().slice(1,-1);
        if(!task || task == ''){
            $.gritter.add({
                title: 'Warning:',
                text: 'Can\'t find this solution' ,
                sticky: false,
                time: 2000
            });
            return ;
        }
        task = JSON.parse(task);

        CanvasJS.initImport('TASK',task);
        CanvasJS.importTask();
    }

    else if(url.indexOf('integration/network/new') != -1){
        NetworkJS.init();
    }

    else if(url.indexOf('visualization/query')!= -1){
        VisualJS.init();
    }

    else if(url.indexOf('visualization/detail') != -1){
        VisualDetailJS.init();
    }

});